/**
 * Copyright (c) 2013, Andrea Funto'. All rights reserved.
 * 
 * This file is part of the Dihedron Concurrent Utilities library ("Concurrent").
 *
 * "Commons" is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 *
 * "Concurrent" is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with "Concurrent". If not, see <http://www.gnu.org/licenses/>.
 */
package org.dihedron.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * Executes the given tasks as background operations in a thread pool, leveraging 
 * JavaSe standard concurrent facilities (executor services etc.).

 * @author Andrea Funto'
 */
public class ThreadPoolExecutor<T> implements TaskExecutor<T> {

	/**
	 * The ExecutorService (from java.lang.concurrent).
	 */
	private ExecutorService service;
	
	/**
	 * Constructor. Remember to invoke dispose() when the object isn't needed 
	 * anymore.
	 *
	 * @param service
	 *   the ExecutorService that will asynchronously process the task in background. 
	 */
	public ThreadPoolExecutor(ExecutorService service) {
		this.service = service;
	}
	
	/**
	 * Assigns the given task to the thread pool for background execution.
	 * 
	 * @param task
	 *   the task to be executed asynchronously.
	 * @return
	 *   the task's Future object, for result retrieval.
	 * @see 
	 *   org.dihedron.concurrent.TaskExecutor#submit(org.dihedron.concurrent.Task)
	 */
	@Override
	public Future<T> submit(Task<T> task) {
		return service.submit(task);
	}

	/**
	 * Closes the task executor, releasing all associated resources.
	 */
	public void dispose() {
		service.shutdown();		
	}	
}
